package com.hngm.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.hngm.bean.Newssheet;
import com.hngm.bean.Newstype;
import com.hngm.common.Result;
import com.hngm.dao.NewssheetDao;
import com.hngm.dao.NewstypeDao;

/**
 * Servlet implementation class Article_list_contentServlet
 */
public class Article_list_contentServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Article_list_contentServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		
		//获取请求中的参数
		String url = request.getParameter("url");
		//创建dao
		NewssheetDao newssheetDao = new NewssheetDao();
		if("add".equals(url)){
			//添加新闻逻辑
			//1、获取前端传过来的信息
			String newssheadlines = request.getParameter("newssheadlines");
			String newscontent = request.getParameter("newscontent");
			String newsprofile = request.getParameter("newsprofile");
			long newstype_id = Long.parseLong(request.getParameter("newstype_id"));
			Newssheet p = new Newssheet(newssheadlines, newscontent, newsprofile, newstype_id);
			//2、产品类型信息插入到表中（调用DAO）
			int result = NewssheetDao.insertNewsconten(p);
			//3、返回结果给前端

			if(result > 0){
				//成功 返回JSON类型的处理结果
				out.print(Result.toClient(true, "添加成功"));
				
			}else{
				//失败
				out.print(Result.toClient(false, "添加失败"));
			}
			
		}else if("del".equals(url)){
			//删除新闻逻辑
			//1、获取前端传过来的产品Id
			String newsId = request.getParameter("newsId");
			//2、根据产品Id删除产品信息
			int result = newssheetDao.delnewssheet(newsId);
			//3、将删除的结果返回给前端页面
			if(result > 0){
				out.print(Result.toClient(true, "删除成功"));
			}else{
				out.print(Result.toClient(false, "删除失败"));
			}
		}else if("edit".equals(url)){
			//编辑新闻逻辑
			//1、获取前端传过来的信息
			long id = Long.parseLong(request.getParameter("id"));
			String newssheadlines = request.getParameter("newssheadlines");
			String newscontent = request.getParameter("newscontent");
			String newsprofile = request.getParameter("newsprofile");
			long newstype_id = Long.parseLong(request.getParameter("newstype_id"));
			Newssheet p = new Newssheet(newssheadlines, newscontent, newsprofile, newstype_id);
			p.setId(id);
			//2、产品类型信息插入到表中（调用DAO）
			int result = NewssheetDao.editNewsconten(p);
			//3、返回结果给前端

			if(result > 0){
				//成功 返回JSON类型的处理结果
				out.print(Result.toClient(true, "编辑成功"));
				
			}else{
				//失败
				out.print(Result.toClient(false, "编辑失败"));
			}
		}
			out.flush();
			out.close();
//		request.getRequestDispatcher("article_list_content.jsp").forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
